"""
验证命令实现

实现validate命令的功能。
"""

import click
from typing import Dict, Any

from .base import BaseCommand
from ...core.validator import DataIntegrityValidator, ValidationResult
from ...core.metadata import MetadataManager


class ValidateCommand(BaseCommand):
    """验证命令实现"""
    
    def __init__(self, config):
        """初始化验证命令"""
        super().__init__(config)
        self.validator = DataIntegrityValidator(self.es_client)
        self.metadata_manager = MetadataManager()
    
    def validate_backup(self, backup_id: str, detailed: bool = False) -> ValidationResult:
        """验证备份
        
        Args:
            backup_id: 备份ID
            detailed: 是否详细验证
            
        Returns:
            验证结果
        """
        try:
            # 检查备份是否存在
            backup_info = self.metadata_manager.get_backup_info(backup_id)
            if not backup_info:
                result = ValidationResult(
                    valid=False,
                    total_checks=1,
                    passed_checks=0,
                    failed_checks=1,
                    warnings=[],
                    errors=[f"备份不存在: {backup_id}"],
                    details={}
                )
                return result
            
            # 执行完整性验证
            return self.metadata_manager.verify_backup_integrity(backup_id)
            
        except Exception as e:
            self.logger.error(f"验证备份失败: {e}")
            return ValidationResult(
                valid=False,
                total_checks=1,
                passed_checks=0,
                failed_checks=1,
                warnings=[],
                errors=[str(e)],
                details={}
            )
    
    def print_validation_result(self, result: ValidationResult):
        """打印验证结果"""
        click.echo(f"\n验证结果:")
        click.echo(f"  总检查项: {result.total_checks}")
        click.echo(f"  通过检查: {result.passed_checks}")
        click.echo(f"  失败检查: {result.failed_checks}")
        
        if result.warnings:
            self.echo_warning("警告:")
            for warning in result.warnings:
                click.echo(f"  - {warning}")
        
        if result.errors:
            self.echo_error("错误:")
            for error in result.errors:
                click.echo(f"  - {error}")
        
        if result.details:
            click.echo(f"\n详细信息:")
            for key, value in result.details.items():
                click.echo(f"  {key}: {value}")
        
        click.echo()
